package com.easyjava.builder;

import com.easyjava.bean.Constants;
import com.easyjava.bean.FieldInfo;
import com.easyjava.bean.TableInfo;
import com.easyjava.utils.StringUtils;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @program: workspace-easyjava
 * @description:
 * @author: cw
 * @create: 2024-05-09 10:57
 **/
public class BuildQuery {

    private static final Logger log = LoggerFactory.getLogger(BuildQuery.class);


    public static void execute(TableInfo tableInfo) {
        File folder = new File(Constants.PATH_QUERY);
        if (!folder.exists()) {
            folder.mkdirs();
        }

        OutputStream os = null;
        OutputStreamWriter osw = null;
        BufferedWriter bw = null;
        try {
            os = new FileOutputStream(folder + "/" + tableInfo.getBeanParamName() + ".java");
            osw = new OutputStreamWriter(os, "UTF-8");
            bw = new BufferedWriter(osw);

            bw.write("package " + Constants.PACKAGE_QUERY + ";\n\n");
            //写入Import
            writeImport(tableInfo, bw);
            //写入类注释
            BuildComment.createClassComment(bw, tableInfo.getComment() + "查询对象");
            bw.write("public class " + tableInfo.getBeanParamName() + " extends BaseQuery {\n\n");

            //写入属性
            writeField(tableInfo.getFieldList(), bw);
            //写入扩展属性
            writeField(tableInfo.getFieldExtendList(), bw);

            //写入getter、setter
            writeGetterSetter(tableInfo.getFieldList(), bw);
            //写入扩展getter、setter
            writeGetterSetter(tableInfo.getFieldExtendList(), bw);

            bw.write("}");
            bw.flush();
        } catch (Exception e) {
            log.error("创建query失败", e);
        } finally {
            BuildBase.close(os, osw, bw);
        }
    }


    private static void writeGetterSetter(List<FieldInfo> fieldList, BufferedWriter bw) throws IOException {
        for (FieldInfo fieldInfo : fieldList) {
            String javaType = fieldInfo.getJavaType();
            String propertyName = fieldInfo.getPropertyName();
            String tempField = StringUtils.upperCaseFirst(propertyName);
            bw.write("\tpublic void set" + tempField + "(" + javaType + " " + propertyName + ") {\n");
            bw.write("\t\tthis." + propertyName + " = " + propertyName + ";\n");
            bw.write("\t}\n");

            bw.write("\tpublic " + javaType + " get" + tempField + "() {\n");
            bw.write("\t\treturn " + propertyName + ";\n");
            bw.write("\t}\n\n");
        }
        bw.newLine();
    }

    private static void writeField(List<FieldInfo> fieldList, BufferedWriter bw) throws Exception {
        for (FieldInfo fieldInfo : fieldList) {
            BuildComment.createFieldComment(bw, fieldInfo.getComment());
            bw.write("\tprivate " + fieldInfo.getJavaType() + " " + fieldInfo.getPropertyName() + ";\n\n");
        }
        bw.newLine();
    }


    private static void writeImport(TableInfo tableInfo, BufferedWriter bw) throws IOException {
        if (tableInfo.getHaveBigDecimal()) {
            bw.write("import java.math.BigDecimal;\n");
        }
        if (tableInfo.getHaveDate() || tableInfo.getHaveDateTime()) {
            bw.write("import java.util.Date;\n");
        }
    }

}
